package com.pz.cloud.framework.pzStudent.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pz.cloud.framework.base.dao.CommonDao;
import com.pz.cloud.framework.base.model.Pageable;
import com.pz.cloud.framework.business.pzStudent.entity.PzStudentPunishment;
import com.pz.cloud.framework.business.pzStudent.service.IPzStudentPunishmentService;
import com.pz.cloud.framework.model.AbstractSqlBuilder;
import com.pz.cloud.framework.model.SqlBuilder;
import com.pz.cloud.framework.pzStudent.mapper.PzStudentPunishmentMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.Map;

@DS("punishment")
@Service
@Transactional
public class PzStudentPunishmentServiceImpl extends ServiceImpl<PzStudentPunishmentMapper, PzStudentPunishment> implements IPzStudentPunishmentService {

    @Autowired
    private CommonDao commonDao;

    @Override
    public IPage<PzStudentPunishment> findPage(PzStudentPunishment entity, Pageable pageable) {
        AbstractSqlBuilder sqlBuilder = SqlBuilder.init(entity, "t");
        return commonDao.findPage(sqlBuilder);
    }

    @Override
    public Integer countByStdNo(String stdNo) {
        PzStudentPunishment entity = new PzStudentPunishment();
        entity.setStudentId(stdNo);
        AbstractSqlBuilder sqlBuilder = SqlBuilder.init("select count(1) num from ").appendTable(PzStudentPunishment.class)
                .append(" where student_id = :stdNo").setParms("stdNo", stdNo)
                .append(" and punish_status != :status").setParms("status", 1)
                .returnType(Count.class);
        Count count = commonDao.findOne(sqlBuilder);
        return count.getNum();
    }

    @Data
    public static class Count {
        private Integer num;
    }
}
